package com.hk.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dpb
 * @date: 2022/3/25
 */
//@Configuration
public class TtlQueueConfig {

    public static final String X_EXCHANGE = "X";
    public static final String QA_QUEUE = "QA";
    public static final String QB_QUEUE = "QB";

    public static final String Y_DEAD_EXCHANGE = "Y";
    public static final String QD_QUEUE = "QD";


    //x交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    //y交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_EXCHANGE);
    }

    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> args = new HashMap<>();
        //声明当前队列绑定到死信队列
        args.put("x-dead-letter-exchange", Y_DEAD_EXCHANGE);
        //死信Rotingkey为YD
        args.put("x-dead-letter-routing-key", "YD");
        //过期时间为10s
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QA_QUEUE).withArguments(args).build();
    }

    @Bean
    public Binding queueaBingX(@Qualifier("queueA") Queue queueA,
                               @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",Y_DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key","YD");
        //过期时间为40s
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QB_QUEUE).withArguments(args).build();
    }
    @Bean
    public Binding queuebBingX(@Qualifier("queueB") Queue queueB,
                               @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    @Bean("queueD")
    public Queue queueD(){
        return new Queue(QD_QUEUE);
    }

    //声明死信队列QD绑定关系
    @Bean
    public Binding deadLetterBindQAD(@Qualifier("queueD") Queue queueD,
                                    @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
}
